/**
 * 病种相关禁忌症管理初始化
 */
var delayReport = {
    id: "delayReportTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    checkPerm: $("#checkPerm").val(), // 审核权限
    submitPerm: $("#submitPerm").val(), // 填报权限
    docUpPerm: $("#formCheckType").val(), // 医生能否直接上报配置
    enableIPQC: $("#enableIPQC").val(), // 是否启用过程质控
};

/**
 * 初始化表格的列
 */
delayReport.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '病案号', field: 'mrNum', align: 'left', valign: 'middle', sortable: true},
        {title: '患者姓名', field: 'name', align: 'left', valign: 'middle', sortable: true},
        {title: '科室', field: 'admLocDesc', align: 'left', valign: 'middle', sortable: true},
        {title: '主管医师', field: 'admDoctorName', align: 'left', valign: 'middle', sortable: true},
        {title: '入组病种', field: 'entityName', width:120, align: 'left', valign: 'middle'},
        {title: '入组状态', field: 'groupStatusDesc', width:100, align: 'center', valign: 'middle'},
        {title: '填报状态', field: 'fillingStatusDesc', width:100, align: 'center', valign: 'middle'},
        {title: '上报状态', field: 'reportStatusDesc', width:100, align: 'center', valign: 'middle'},
        {title:'表单填充率',field:'ECount',width: 120,align:'center',
            formatter: function (value, row) {
                var tempval = ((parseInt(row.ECount) * 100) / (parseInt(row.ECount) + parseInt(row.NCount))).toFixed(1);
                if (isNaN(tempval)) tempval = 0
                var htmlstr = '<div class="hisui-tooltip" title="' + tempval + '%" style="width:100%; height:20px;border:1px solid #B2BBBE;border-radius: 5px;position: relative;">' +
                    '<div style="width: ' + tempval + '%; height:18px;line-height: 18px;background-color:#50c878;color:#FFFFFF;border-radius: 4px;">' +tempval + '%</div></div>';
                return htmlstr;
            }
        },
        {title: '迟报天数', field: 'reportDelayDays', width:100, align: 'center', valign: 'middle', formatter:function(value,row,index){
                if (7 <= value && value < 10) {
                    return "<div style='background-color:#0099CC;color: #FFFFFF;'>" + value + "</div>";
                } else if (10 <= value && value < 30) {
                    return "<div style='background-color:#FFFF00;color: #FFFFFF;'>" + value + "</div>";
                } else if (30 <= value && value < 90) {
                    return "<div style='background-color:#FF9900;color: #FFFFFF;'>" + value + "</div>";
                } else if (90 <= value) {
                    return "<div style='background-color:#CC0066;color: #FFFFFF;'>" + value + "</div>";
                } else {
                    return "<div style='background-color:#66CCCC;color: #FFFFFF;'>" + value + "</div>";
                }
            }},

        {title: '入院日期', field: 'admDate', align: 'left', valign: 'middle', sortable: true},
        {title: '出院日期', field: 'dishDate', align: 'left', valign: 'middle', sortable: true},
        {title: '入单日期', field: 'inDate', align: 'left', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',
            formatter: function (value, row, index) {
                var _html = '<a href="javascript:delayReport.edit(\'' + row.episodeId + '\',\'' + row.id + '\',\''+row.name+'\',\''+row.mrNum+'\',\''+row.dishDate+'\',\''+row.fillingStatusDesc+'\')" title="填报"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:delayReport.del(\'' + row.id + '\',\''+row.entityId+'\')" title="排除"><i class="hisui-icon icon-cancel"></i></a>';
                _html += '<a href="javascript:delayReport.record(\'' + row.id + '\',\'' + row.mrNum + '\',\'' + row.name + '\')" title="日志"><i class="hisui-icon icon-clock-record"></i></a>';
                return _html;
            }
        }];
};

/**
 * 检查是否选中
 */
delayReport.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        delayReport.seItem = selected[0];
        return true;
    }
};

/**
 * 打开查看病种入组病例详情
 */
delayReport.edit = function (episodeId, id, name, mrNum, dishDate, state) {
    var flag = false;
    if (id) {
        flag = true;
    }else if (delayReport.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        // 未启用过程质控模块直接打开填报表单
        if (delayReport.enableIPQC == 'Y') {
            delayReport.GetMrListIfo(episodeId, id, name, mrNum, dishDate, state);
        } else {
            delayReport.openFormWin(id, name, mrNum, dishDate, state);
        }
    }
};

delayReport.GetMrListIfo = function (episodeId, mrListId, name, mrNum, dishDate, stateDesc) {
    var mrlistInfo;
    var ajax = new $ax("/sds/sdsqcincase/getMrListInfo", function (data) {
        mrlistInfo = data;
    }, function () {
    });
    ajax.setData({"episodeId": episodeId});
    ajax.start();
    if (!$.isEmptyObject(mrlistInfo)) {
        var mrListId = mrlistInfo.mrlistIdOut;
        var entityIdOut = mrlistInfo.entityIdOut;
        var entityNameOut = mrlistInfo.entityNameOut;
        var entityIdIn = mrlistInfo.entityIdIn;
        var entityNameIn = mrlistInfo.entityNameIn;
        var tipsStr = "\t<p style='color:#ff7e00;background-color:#fff3dd;border: 1px solid #f7d199;text-indent:2em;'>该患者在院质控病种和出院入组筛查病种不一致，请选择并确认上报病种！</p>\n";
        tipsStr += "\t<div style='display: block;padding: 5px;'>\n"
        tipsStr += "\t<label>出院筛查病种：</label><input type=\"radio\" name=\"entityId\" value=\"" + entityIdOut + "\" checked>\n" + "<span style='color: #29723C'>" + entityNameOut + "</span>";
        tipsStr += "\t<br>\n";
        tipsStr += "\t<label>在院质控病种：</label><input type=\"radio\" name=\"entityId\" value=\"" + entityIdIn + "\">\n" + "<span style='color: #29723C'>" + entityNameIn + "</span>";
        tipsStr += "\t</div>\n";
        $("#mrListInfoHtml").html(tipsStr);
        var titleIndex = layer.open({
            type: 1,
            title: ['单病种填报提醒', 'padding-left:10px;background:#556983;color:white;'],
            content: $("#mrListInfoDiv"),
            area: ['350px', '250px'], //宽高
            fix: false, //不固定
            maxmin: false,
            skin: 'layer-ext-hisui',
            zIndex: 1100,
            btn: ['确定'],
            btnAlign: 'c',
            yes: function (index, layero) {
                layer.close(titleIndex);
                $.messager.progress({title: "提示", msg: '变更病种并自动采集填报数据', text: '采集中....'});
                delayReport.changeMrEntity(mrListId, $('input[name="entityId"]:checked').val());
                $.messager.progress('close');
                delayReport.openFormWin(mrListId, name, mrNum, dishDate, stateDesc);
            }
        });
    } else {
        delayReport.openFormWin(mrListId, name, mrNum, dishDate, stateDesc)
    }
};

delayReport.changeMrEntity = function (mrListId, entityId) {
    var ajax = new $ax("/sds/sdsqcincase/changeMrEntity", function (data) {
    }, function (data) {
    });
    ajax.setData({"mrListId": mrListId, "entityId": entityId});
    ajax.start();
};

// 打开表单填报窗口
delayReport.openFormWin = function (mrListId, name, mrNum, dishDate, state) {
    var title = "姓名:" + name + "|" + "病案号:" + mrNum + "|" + "出院日期:" + dishDate + "|" + "填报状态:" + state;
    var index = Sys.open_c(title, '/sds/sdsqcformshow?mrListId=' + mrListId + '&checkPerm=' + delayReport.checkPerm + '&submitPerm=' + delayReport.submitPerm+ '&reUploadFlag=' + (state === "上报失败" ? 1 : 0), null, function () {
        delayReport.table.refresh();
    });
    this.layerIndex = index;
}

/**
 * 打开病种表单(双击行)
 */
delayReport.qcFormShow = function (r, e, f) {
    // 未启用过程质控模块直接打开填报表单
    if (delayReport.enableIPQC == 'Y') {
        delayReport.GetMrListIfo(r.episodeId, r.id, r.name, r.mrNum, r.dishDate, r.stateDesc);
    } else {
        delayReport.openFormWin(r.id, r.name, r.mrNum, r.dishDate, r.stateDesc);
    }
}

/**
 * 排除病种入组病例
 */
delayReport.del = function (id, entityId) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        entityId = this.seItem.entityId;
        flag = true;
    }
    if (flag) {
        //打开 弹出框
        var index = layer.open({
            type: 2,
            title: ['排除单病种【信息录入】'],
            area: ['600px', '400px'],
            zIndex: 19911102,//这是个大坑！！！！ 默认是19891014 远远大于量表下拉框的zIndex 导致下拉框的数据看不到！！！
            content: "/sds/sdsqcformshow/excludereason?mrListId=" + id + "&entityId=" + entityId,
            skin: 'layer-ext-hisui',
            scrollbar: false//为了禁止父页面滚动条滚动
        });
        this.layerIndex = index;

        //监听子页面的postMessage
        function receiveMessage(event) {
            //取得子页面传回来的值
            if (event.data == "close") {
                // 如果传回close，关闭当前弹窗
                layer.close(index);
            }
            if (event.data == "save") {
                delayReport.search2();
                setTimeout(function () {
                    layer.close(index);
                }, 1000);
            }
        }
        window.addEventListener('message', receiveMessage, false);
    }
};
delayReport.search=function () {

    var entityId=$('#entityType').combobox('getValue');
    var queryData={};
    queryData['entityId']=entityId;
    delayReport.table.refresh({query: queryData, pageNumber: 1});
    
}
delayReport.search2 = function () {
    delayReport.table.refresh();
}
delayReport.record = function (id, mrno, name) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('【填报日志】姓名：' + name + ' 病案号：' + mrno, Sys.backendPath + '/sds/sdsqcincaseoperlog/' + id, ['800px', '420px']);
        this.layerIndex = index;
    }
}
 

$(function () {
    var defaultColunms = delayReport.initColumn();
    var table = new BSTable(delayReport.id, Sys.backendPath + "/sds/sdsqcincase/getDelayReport", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setShowRefresh(false);
    table.setShowColumns(false);
    table.setShowToggle(false);
    table.setShowExport(false);
    table.setSubHeight(-27);
    table.setOnDblClickRow(delayReport.qcFormShow); //双击数据行打开表单填报
    delayReport.table = table.init();

    //病种
    $('#entityType').combobox({
        data: $.parseJSON($("#entityMap").val()),
        valueField: 'value',
        textField: 'text',
        defaultFilter: 6
    });
});
